<?php

/**
 *  @author  Lynn Cho <lynncho@live.com>
 *  @createTime  2016-9-1 14:24:48
 *  @copyright  shewen technology
 *  @descrition  
 */
class SystemModel extends BaseModel {

    public function __construct() {
        $this->load->library('manager/Permission');
    }

    /**
     * get current login user all permission
     * @return array
     */
    public function getPermission() {
        $id = $this->session->manager->id;
        if (is_numeric($id)) {
            $permission = $this->db
                    ->where('mid', $id)
                    ->select('prvid')
                    ->get('funcprv')
                    ->result_array();
            if (empty($permission)) {
                return false;
            } else {
                foreach ($permission as $val) {
                    $hasPermission[(int) $val['prvid']] = 1;
                }
                return $hasPermission;
            }
        }
    }

    /**
     * get all function for views
     * @return array
     * @how-to-use $id = $func[funcname]
     */
    public function getFuncIDAndName() {
        $func = $this->db
                ->select('funcname', 'id')
                ->get('cfunction')
                ->result_array();
        if (empty($func)) {
            return false;
        } else {
            foreach ($func as $val) {
                $funcid[$val['funcname']] = $val['id'];
            }
            return $funcid;
        }
    }

    /**
     * tell hello by hours
     * @timezone PRC setted in config.php
     * @return string
     */
    public function timeHello() {
        $h = date('G.i');
        if ($h < 8.30)
            return '早起的鸟儿有虫吃';
        else if ($h < 11.45)
            return '上午好';
        else if ($h < 12.30)
            return '该吃午饭了';
        else if ($h < 17)
            return '下午好';
        else if ($h < 20)
            return '晚上好';
        else
            return '您辛苦了';
    }

    /**
     * get manager user
     * @param string $username
     * @param int $type
     * @param int $rows Rows num in each page
     * @param int $page
     * @return array
     */
    public function getManagerList($username, $power, $rows = 10, $page = 1) {
        if (!empty($username)) {
            $where['username'] = $username;
        }
        if (is_numeric($power)) {
            if ($power !== '999') {
                $where['power'] = (int) $power;
            }
        }

        if (!empty($where)) {
            $this->db->where($where);
        }
        $re['total'] = $this->db->where('delete', 0)->get('manager')->num_rows();

        if (!empty($where)) {
            $this->db->where($where);
        }
        $re['rows'] = $this->db
                ->where('delete', 0)
                ->select(array('id', 'username', 'power', 'phone', 'email', 'note'))
                ->limit($rows, $rows * ($page - 1))
                ->get('manager')
                ->result_array();

        $re['rows'] = $this->permission->powerWords($re['rows']);

        return $re;
    }

    public function saveManager($array) {
        $array['pwd'] = $this->permission->customMD5($array['pwd']);
        $array['id'] = $this->createID('manager', 'id');
        if (empty($array['id'])) {
            return 500;
        }
        $powerlist = $array['powerlist'];
        unset($array['powerlist']);
        $grouplist = $array['grouplist'];
        unset($array['grouplist']);
        $array['power'] = $this->permission->markingPower($powerlist);

        $this->db->trans_begin();
        $this->db->insert('manager', $array);
        if (is_array($powerlist)) {
            $this->db->where('mid', $array['id'])->delete('funcprv');
            foreach ($powerlist as $key => $val) {
                $power[$key]['mid'] = $array['id'];
                $power[$key]['prvid'] = $val;
            }
            $this->db->insert_batch('funcprv', $power);
        }
        if (is_array($grouplist)) {
            $this->db->where('mid', $array['id'])->delete('managergroup');
            foreach ($grouplist as $key => $val) {
                $group[$key]['mid'] = $array['id'];
                $group[$key]['groupid'] = $val;
            }
            $this->db->insert_batch('managergroup', $group);
        }
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 500;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function editManager($array) {
        if (empty($array['pwd'])) {
            unset($array['pwd']);
        } else {
            $array['pwd'] = $this->permission->customMD5($array['pwd']);
        }
        if (empty($array['id'])) {
            return 403;
        }

        $powerlist = $array['powerlist'];
        unset($array['powerlist']);
        $grouplist = $array['grouplist'];
        unset($array['grouplist']);
        $array['power'] = $this->permission->markingPower($powerlist);

        $this->db->trans_begin();
        $this->db->where('id', $array['id'])->update('manager', $array);

        if (is_array($powerlist)) {
            $this->db->where('mid', $array['id'])->delete('funcprv');
            foreach ($powerlist as $key => $val) {
                $power[$key]['mid'] = $array['id'];
                $power[$key]['prvid'] = $val;
            }
            $this->db->insert_batch('funcprv', $power);
        }
        if (is_array($grouplist)) {
            $this->db->where('mid', $array['id'])->delete('managergroup');
            foreach ($grouplist as $key => $val) {
                $group[$key]['mid'] = $array['id'];
                $group[$key]['groupid'] = $val;
            }
            $this->db->insert_batch('managergroup', $group);
        }

        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
            return 500;
        } else {
            $this->db->trans_commit();
            return true;
        }
    }

    public function deleteManager($id) {
        if (is_numeric($id)) {
            $this->db->trans_begin();
            $this->db->where('id', $id)->update('manager', array('delete' => 1));
            $this->db->where('mid', $id)->delete('funcprv');
            if ($this->db->trans_status() === FALSE) {
                $this->db->trans_rollback();
                return false;
            } else {
                $this->db->trans_commit();
                return true;
            }
        } else {
            return 301;
        }
    }

    public function getGeneralPermission() {
        return $this->permission->getGeneralPermissionList();
    }

    public function getDetailPermissionTree() {
        return $this->db->get('cfunction')->result_array();
    }

}
